package com.webshop.DataDao;

import com.webshop.JavaBean.Goods_attr;
import com.webshop.JavaBean.Shopcar;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddShopcarDao {
    QueryRunner queryRunner= new QueryRunner(DataSourceUtils.getDataSource());
    //加入购物车
    public void insert(Shopcar shopcar)throws Exception{
        String sql="insert into shopcar (userName,goodsName,nums,pic,price,goodsAttrID,goodsAttr)" +
                " values(?,?,?,?,?,?,?)";
        Object[] objects={shopcar.getUserName(),shopcar.getGoodsName(),shopcar.getNums(),
                shopcar.getPic(),shopcar.getPrice(),shopcar.getGoodsAttrID(),shopcar.getGoodsAttr()};
        queryRunner.update(sql,objects);
    }
    //当加入购物车是寻找是否有和它相同的记录
    public Shopcar find(Shopcar shopcar)throws Exception{
        String sql="select * from shopcar where userName = ?"+" and goodsName= ? "+
                " and pic = ?"+
                " and price =? "+
                " and goodsAttrID = ?"+
                " and goodsAttr = ?";
        Object[] objects={shopcar.getUserName(),shopcar.getGoodsName(),shopcar.getNums(),
                shopcar.getPic(),shopcar.getPrice(),shopcar.getGoodsAttrID(),shopcar.getGoodsAttr()};
        Shopcar s=new Shopcar();
           s=queryRunner.query(sql,new BeanHandler<>(Shopcar.class),objects);
           return s;
    }
    //更新数量
    public  void modify(Shopcar shopcar) throws Exception{
        String sql="update shopcar set nums = ?"+" where userName = ?"+
                " and goodsName= ? "+
                " and price =? "+
                " and goodsAttrID = ?"+
                " and goodsAttr = ?";
        Object[] objects={shopcar.getNums(),shopcar.getUserName(),shopcar.getGoodsName(),shopcar.getPrice(),
                shopcar.getGoodsAttrID(),shopcar.getGoodsAttr()};
        queryRunner.update(sql,objects);
    }

    /**
     * 根据购物车ID返回单一的购物车
     * @param ID
     * @return
     */
    public Shopcar backShopCar(String ID) {
        String sql = "select * from shopcar where ID=?";
        Shopcar query = null;
        try {
            query = queryRunner.query(sql, new BeanHandler<>(Shopcar.class), ID);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }


    //将购物车东西加入页面
    public List<Shopcar> buyfind(String name)throws Exception{
        String Sql="select * from shopcar where userName = ?";
         List<Shopcar> shopcar=new ArrayList<>();
         shopcar=queryRunner.query(Sql,new BeanListHandler<>(Shopcar.class),name);
         return shopcar;
    }
    //删除购物车的商品
    public void deleter(int ID) throws Exception{
        String sql="delete from shopcar where ID = ?";
        Object[] o={ID};
        queryRunner.update(sql,o);
    }


    /**
     * 根据商品的详情ID返回购物车
     * @param ID
     * @param name
     * @return
     */
    public Shopcar backShopCarDependAttrID(String ID,String name) {
        String sql = "select * from shopcar where goodsAttrID =? and userName =? limit 0,1";
        Shopcar query = null;
        try {
            query = queryRunner.query(sql, new BeanHandler<>(Shopcar.class), ID, name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

}
